<template>
<div class="card-view" >
	<!-- 左边 -->
	<template v-for="(items, index) in [leftItems, rightItems]">
	<div v-if="index === 0 || index === 1">
		<div class="item-card fadeIn" v-for="(item,index) in items" :key="index" @click="juMp(item._id)">
			<div class="cover-drawing">
				<img :src="item.cover_image.url" class="cover-image"/>
				<div class="cover-adress global-display global-a-items">
					<img src="@/icon/index/address.png"/>
					<span class="text-show">{{item.address}}</span>
				</div>
			</div>
			<p class="cart-Title-text text-show">{{item.title}}</p>
			<div class="global-display global-j-content">
				<div class="card-name global-display global-a-items" v-for="(item_a,index_a) in item.author_data">
					<img :src="item_a.avatarUrl" alt="" class="avatarUrl">
					<p>{{item_a.nickname}}</p>
				</div>
				<div class="card-name global-display global-a-items">
					<img src="@/icon/index/page-zan.png" alt="" class="page-zan">
					<p>{{item.likes}}</p>
				</div>
			</div>
		</div>
	</div>
	</template>
</div>
	
<!-- 如果没有数据 -->
<van-empty
	v-if="exist"
	image="https://diancan-1252107261.cos.accelerate.myqcloud.com/lvyou/icon/kong-zhuang-tai.png"
	image-size="80"
	description="没有更多啦"
/>
</template>

<script setup>
import { ref,onMounted,reactive,watch, } from 'vue'
const props = defineProps({
	userTravels: {type:Array,required:true}
})
import { useRouter,useRoute } from 'vue-router'
const $router = useRouter()  // 这是路由跳转的
const $route = useRoute();
import emitter from '@/api/event.js'

// 左侧和右侧的数据列表
let leftItems = ref([]);
let rightItems = ref([]);
// 左侧和右侧的高度
let leftHeight = ref(0);
let rightHeight = ref(0);

const exist = ref(false)

watch(props,(newValue,oldValue)=>{
	// 将初始图片列表按照高度均衡地分配到左右两侧
	const {userTravels} = newValue
	leftItems.value = [];
	rightItems.value = [];
	leftHeight.value = 0;
	rightHeight.value = 0;
	if(userTravels.length > 0){
		userTravels.forEach(item => {
		  if (leftHeight.value <= rightHeight.value) {
		    leftItems.value.push(item);
		    leftHeight.value += Number(handleImageLoad(item.cover_image.width,item.cover_image.height));
		  } else {
		    rightItems.value.push(item);
		    rightHeight.value += Number(handleImageLoad(item.cover_image.width,item.cover_image.height));
		  }
		});
		// console.log(userTravels);
	}
	exist.value = leftItems.value.length > 0 ? false : true
})

function juMp(_id){
	// 获取跳转的路由：如果用户点击了详情页的推荐就向详情页触发响应
	if($route.path === '/details-page'){
		emitter.emit('details-response',{_id});
		$router.replace({
		  path: $route.path,
		  query: Object.assign({}, $route.query, { _id })
		});
	}else{
		$router.push({path: '/details-page', query: {_id}})
	}
}

function handleImageLoad(realImageWidth,realImageHeight){
	const rootFontSize = parseFloat(getComputedStyle(document.documentElement).fontSize);
	const screenWidth = window.innerWidth // 屏幕宽度
	const itemPadding = 5 // 每个元素的padding值
	const columnGap = 17 // 元素之间的间隙，单位为px
	const columnCount = 2 // 列数
	const containerWidth = screenWidth - (itemPadding + columnGap) * (columnCount - 1) - itemPadding * 2; // grid容器的宽度
	const itemWidth = Math.floor(containerWidth / columnCount); // 每列元素的宽度
	const imageRatio = realImageHeight / realImageWidth; // 图片高宽比例
	const itemHeight = itemWidth * imageRatio; // 元素在像素px单位下的高度
	// console.log(itemHeight.toFixed(0));
	return itemHeight.toFixed(0)
}
</script>

<style scoped>
/* ----------- */
.card-view{
	padding: 0 5px;
	display: grid; 
	grid-template-columns: repeat(2, 1fr);
}
.item-card{
	padding: 5px;
	box-sizing: border-box !important;
}
.data-left {
    grid-column: 1 / 2;
}
.data-right {
	grid-column: 2 / 3;
}
.cover-drawing{
	position: relative;
	border-radius: 10px;
	overflow: hidden;
}
.cover-image{
	width: 100%;
	display: block;
	/* border-radius: 10px; */
	object-fit: cover;
}
.cover-adress{
	position: absolute;
	bottom: 0;
	left: 0;
	right: 0;
	padding: 0 0 10px 10px;
	background: linear-gradient(to bottom, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.8));
}
.cover-adress img{
	width: 12px;
	height: 12px;
	display: block;
}
.cover-adress span{
	font-size: 10px;
	font-weight: bold;
	color: #ffffff;
	padding-left: 5px;
}
.cart-Title-text{
	font-size: 14px;
	font-weight: bold;
	padding-top: 7px;
	line-height:1.4;
}
.card-name{
	padding: 7px 0;
}
.avatarUrl{
	width: 20px;
	height: 20px;
	object-fit: cover;
	border-radius: 50%;
}
.page-zan{
	width: 15px;
	height: 15px;
}
.card-name p{
	padding-left: 6px;
	font-size: 13px;
	color: #717375;
}
</style>